package com.yx.cloud.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.yx.cloud.R;
import com.iot.common.utils.LogUtils;

public class CircleRelativeLayout extends View {
    private int color;
    private Bitmap bitmap;
    private Bitmap cache;
    private Paint paint;
    private RadialGradient gradient;
    private float radius;
    private Matrix matrix;
    private float radiusHeight;

    public CircleRelativeLayout(Context context) {
        super(context);
    }

    public CircleRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
        setWillNotDraw(false);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CircleRelativeLayout);
        color = array.getColor(R.styleable.CircleRelativeLayout_color, 0x88ffffff);
        int res = array.getResourceId(R.styleable.CircleRelativeLayout_image, 0);
        radius = array.getFloat(R.styleable.CircleRelativeLayout_light_radius, 250);
        array.recycle();

        if (res == 0) {
            bitmap = null;
        } else {
            bitmap = BitmapFactory.decodeResource(getResources(), res);
            radius=bitmap.getWidth()*3/5;
        }
        cache=Bitmap.createBitmap((int)radius*2,(int)radius*2, Bitmap.Config.ARGB_8888);
        matrix=new Matrix();
        matrix.setScale(1,0.7f);
        radiusHeight=radius*0.7f;
        LogUtils.i("res : " + res);

        paint = new Paint();
        paint.setColor(color);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;
        float maxWidth = bitmap.getWidth();
        if (maxWidth < radius * 2) {
            maxWidth = radius * 2;
        }
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            width = (int) maxWidth;
        } else {
            width = (int) maxWidth;
        }

        float maxHeighth = bitmap.getHeight();

        if (bitmap == null) {
            maxHeighth = radiusHeight * 2;
        } else {
            if (radiusHeight < bitmap.getHeight() * 0.1f) {
                maxHeighth = bitmap.getHeight();
            } else if (radiusHeight < bitmap.getHeight() * 0.9f) {
                maxHeighth = bitmap.getHeight() * 0.9f + radiusHeight;
            } else {
                maxHeighth = radiusHeight*2;
            }
        }


        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            height = (int) maxHeighth;
        } else {
            height = (int) maxHeighth;
        }

        LogUtils.i("w: " + width + " h: " + height);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) { //构建圆形
//        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();
        LogUtils.i("w: " + width + " h: " + height);

        float offsetX = 0;
        float offsetY = 0;

        if (gradient == null) {
            gradient = new RadialGradient(width / 2, radius, radius, color, Color.TRANSPARENT, Shader.TileMode.CLAMP);
            paint.setShader(gradient);
        }

        canvas.save();
        canvas.setMatrix(matrix);
        canvas.drawCircle(width / 2, radius, radius, paint);
        canvas.restore();

        if (bitmap == null) {

        } else {
            offsetX = (width - bitmap.getWidth()) / 2;
            offsetY = height - bitmap.getHeight();

            canvas.drawBitmap(bitmap, offsetX, offsetY, paint);
        }



    }

    public int getColor() {
        return paint.getColor();
    }

    public void setColor(int color) { //设置背景色
        this.color = color;
        gradient=null;
        invalidate();
    }

}
